package com.summer.controller;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbActivityType;
import com.summer.entity.TbActivityinfo;
import com.summer.entity.TbRole;
import com.summer.entity.TbUserinfo;
import com.summer.mapper.TbActivityTypeMapper;
import com.summer.mapper.TbActivityinfoMapper;
import com.summer.mapper.TbUserinfoMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>还没测试
 *测试完成20:20
 * @author summer502
 * @since 2021-03-31
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-activity-type")
@Transactional
@Api(value = "TbActivityTypeController", description = "活动类型api(及活动分类api)")

public class TbActivityTypeController {
    /**
     *  status
     *  typeName
     */
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired(required =false)
    private TbActivityTypeMapper tbActivityTypeMapper;
    HttpServletRequest request;
    @Autowired (required = false)//https://blog.csdn.net/qq_40147863/article/details/86103857
    TbUserinfoMapper tbUserinfoMapper;
    @Autowired (required = false)
    TbActivityinfoMapper tbActivityinfoMapper;
    /**
     * 超级管理员添加活动分类
     * @param type_name
     * @param Authorization
     * @return
     * @throws IOException
     * @throws TokenExpiredException
     */
    @ApiOperation("超级管理员添加活动分类")
    @PostMapping("/teacher/addActivityType")
    @ResponseBody
    public ResponseEntity addActivityType(@RequestParam(required = true) String type_name,
                                     @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        if (tbUserinfo.getUserId()!=1&&tbUserinfo.getUserId()>0){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","权限错误");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
        //根据user_jobNumber查询数据库是否有该用户存在，不存在则注册，存在则注册失败
        QueryWrapper<TbActivityType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_name", type_name);
        TbActivityType tbActivityType = tbActivityTypeMapper.selectOne(queryWrapper);
        //User userFromBase = userDao.selectById(user_name);
        if (tbActivityType == null){
            responseEntity = ResponseEntity.ok();
            TbActivityType tbActivityType1 = new TbActivityType();
            tbActivityType1.setTypeName(type_name);
            tbActivityType1.setStatus(1);//1开启
            tbActivityTypeMapper.insert(tbActivityType1);
            responseEntity.putDataValue("type_id",tbActivityType1.getTypeId());
            responseEntity.putDataValue("msg","活动分类添加成功");
        }else {
            responseEntity = ResponseEntity.badRequest();
            responseEntity.putDataValue("msg","活动分类添加失败");
        }
        return responseEntity;
    }

    /**
     * 超级管理员修改活动分类信息
     * @param type_id
     * @param type_name
     * @param Authorization
     * @return
     * @throws IOException
     */

    @ApiOperation("超级管理员修改活动分类信息")
    @PutMapping("/web/updateActivityType")
    @ResponseBody
    public ResponseEntity updateActivityType(@RequestParam(required = true) int type_id, @RequestParam(required = false) String type_name, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()!=1) {
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "没有权限");
            return responseEntity;

        } else {
            responseEntity = ResponseEntity.ok();
            TbActivityType tbActivityType = tbActivityTypeMapper.selectById(type_id);
            QueryWrapper<TbActivityType> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("type_name",type_name);
            TbActivityType tbActivityType1 = tbActivityTypeMapper.selectOne(queryWrapper);
            if(tbActivityType1==null){
                tbActivityType.setTypeName(type_name);
                tbActivityTypeMapper.updateById(tbActivityType);
                responseEntity.putDataValue("msg", "活动分类修改成功");
                return responseEntity;
            }
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "活动分类名已经存在");
            return responseEntity;

        }
    }

    /**
     * 根据type_id查询活动分类
     * @param type_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据type_id查询活动分类")
    @PutMapping("/web/selectByActivityTypeId")
    @ResponseBody
    public ResponseEntity selectByActivityTypeId(@RequestParam(required = true) Integer type_id, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()==1) {
            responseEntity=ResponseEntity.ok();
            TbActivityType tbActivityType = tbActivityTypeMapper.selectById(type_id);
            responseEntity.putDataValue("tbActivityType",tbActivityType);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 根据id删除活动分类信息
     * @param type_id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除活动分类信息")
    @PutMapping("/web/deleteActivityType")
    @ResponseBody
    public ResponseEntity deleteActivityType(@RequestParam(required = true) Integer type_id,@RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users=JwtToken.unsign(token,TbUserinfo.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getRoleId()==1) {
            TbActivityType tbActivityType = tbActivityTypeMapper.selectById(type_id);
            if(tbActivityType!=null){
                int is_delete = 1;
                QueryWrapper<TbActivityinfo> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("activity_type_name",tbActivityType.getTypeName());
                List<TbActivityinfo> list=tbActivityinfoMapper.selectList(queryWrapper);
               // int role_id1=0;
                String activity_type_name="未分类";
                for (TbActivityinfo tbUserinfo : list) {
                    tbUserinfo.setActivityTypeName(activity_type_name);
                    tbActivityinfoMapper.updateById(tbUserinfo);
                }
                tbActivityType.setIsDelete(is_delete);
                tbActivityTypeMapper.updateById(tbActivityType);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                return responseEntity;
            }
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }
    /**
     * 活动分类列表
     * @return
     * @throws IOException
     */
    @ApiOperation("活动分类列表")
    @PostMapping("/web/ActivityTypesList")
    @ResponseBody
    public ResponseEntity ActivityTypesList() throws IOException {

        ResponseEntity responseData;
        QueryWrapper<TbActivityType> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<TbActivityType> list = tbActivityTypeMapper.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("list",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }

    /**
     * 统计活动分类数量
     */
    @ApiOperation("统计活动分类数量")
    @GetMapping("/web/count")
    @ResponseBody
    public ResponseEntity count() throws IOException {
        ResponseEntity responseData;
        int is_delete = 0;
        QueryWrapper<TbActivityType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", is_delete);
        int  count =tbActivityTypeMapper.selectCount(queryWrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("count", count);
        return responseData;
    }
}
